package com.yxhl.km.module.person;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.yxhl.km.HomeActivity;
import com.yxhl.km.R;
import com.yxhl.km.base.BaseFragment;
import com.yxhl.km.module.entity.EventBean;
import com.yxhl.km.module.login.activity.LoginActivity;
import com.yxhl.km.module.login.entity.UserInfoBean;
import com.yxhl.km.module.login.mvp.LoginPrensenter;
import com.yxhl.km.module.person.activity.EditPersonActivity;
import com.yxhl.km.module.person.adapter.DataAdapter;
import com.yxhl.km.module.person.entity.ItemModel;
import com.yxhl.km.module.person.entity.UserInfo;
import com.yxhl.km.module.person.mvp.PersonPrensenter;
import com.yxhl.km.module.utils.Constants;
import com.yxhl.km.module.utils.LogUtil;
import com.yxhl.km.module.utils.MD5Utils;
import com.yxhl.km.module.utils.PicassoUtil;
import com.yxhl.km.module.utils.SharedPreferencesUtils;
import com.yxhl.km.module.utils.ToastUtil;
import com.yxhl.km.module.view.DiagUtil;
import com.yxhl.km.module.widget.CircleImageView;
import com.yxhl.km.mvp.BaseView;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张亚楠 on 2018/5/28.
 */

public class PersonFragment extends BaseFragment {
    private List<ItemModel> list = new ArrayList<>();
    private LRecyclerView mRecyclerView;
    private DataAdapter mDataAdapter = null;
    private LRecyclerViewAdapter mLRecyclerViewAdapter = null;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_person;
    }

    @Override
    protected void initListener() {
        titlebar_right_text.setOnClickListener(this);
        rl_perdion_head.setOnClickListener(this);
    }

    @Override
    protected void initialized() {
        initData();
        initView();
    }

    private void initData() {
        list.clear();
        list.add(new ItemModel("修改密码", R.mipmap.ic_mine_pwd));
        list.add(new ItemModel("版本更新", R.mipmap.ic_mine_version));
        list.add(new ItemModel("清除缓存", R.mipmap.ic_clear));
        list.add(new ItemModel("常见问题", R.mipmap.ic_my_question));
        list.add(new ItemModel("意见反馈", R.mipmap.ic_mine_backinfo));
        list.add(new ItemModel("历史运单", R.mipmap.ic_mine_task_record));
        list.add(new ItemModel("历时取样", R.mipmap.ic_histroy_sampling));
    }

    private void initView() {
        initTitle("", "我的", "退出");
        initList();
        initHead();
        requstUser();
    }

    private void requstUser() {
        DiagUtil.getInstance().createWaitDialog(getActivity());
        PersonPrensenter personPrensenter = new PersonPrensenter(new BaseView<UserInfo>() {
            @Override
            public void onResult(UserInfo userInfo) {
                DiagUtil.getInstance().closeWaitDialog();
                LogUtil.e("UserInfoBean信息：" + getUserInfo().toString());
                LogUtil.e("UserInfo信息：" + userInfo.toString());
                if (!TextUtils.isEmpty(userInfo.getData().getName())) {
                    tv_name.setText(userInfo.getData().getName());
                }
                if (!TextUtils.isEmpty(userInfo.getData().getPhone())) {
                    tv_phone.setText(userInfo.getData().getPhone());
                }
                if (!TextUtils.isEmpty(userInfo.getData().getHeadimg())) {
                    PicassoUtil.LoadPicasso(getActivity(), userInfo.getData().getHeadimg(), iv_head);
                }

            }
        });
        if (!TextUtils.isEmpty(getUserInfo().getData().getUser().getId())) {
            personPrensenter.getUserData(getUserInfo().getData().getUser().getId());
        }
    }

    private TextView tv_phone, tv_name;
    private CircleImageView iv_head;
    private AutoRelativeLayout rl_perdion_head;

    private void initHead() {
        final View header = LayoutInflater.from(getActivity()).inflate(R.layout.persion_head, (ViewGroup) getActivity().findViewById(android.R.id.content), false);
        tv_phone = (TextView) header.findViewById(R.id.tv_phone);
        tv_name = (TextView) header.findViewById(R.id.tv_name);
        iv_head = (CircleImageView) header.findViewById(R.id.iv_head);
        rl_perdion_head = (AutoRelativeLayout) header.findViewById(R.id.rl_perdion_head);
        mLRecyclerViewAdapter.addHeaderView(header);
    }

    /**
     * EventBus修改个人信息
     *
     * @param eventBean
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onMoonEvent(EventBean eventBean) {
        if (eventBean.getType().equals(Constants.EVENTVUS_EDIT_ICON)) {
            String headUrl = (String) eventBean.getT();
            PicassoUtil.LoadPicasso(mContext, headUrl, iv_head);
        }
    }

    private void initList() {
        mRecyclerView = (LRecyclerView) view.findViewById(R.id.list);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        mRecyclerView.setLayoutManager(manager);
        mDataAdapter = new DataAdapter(getActivity());
        mDataAdapter.addAll(list);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        mRecyclerView.setAdapter(mLRecyclerViewAdapter);
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.colorAccent, R.color.dark, android.R.color.white);
        //设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.colorAccent, R.color.dark, android.R.color.white);
        //设置底部加载文字提示
        mRecyclerView.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        //根据需要选择使用GridItemDecoration还是SpacesItemDecoration
//        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
//                .setHeight(R.dimen.default_divider_padding)
//                .setPadding(R.dimen.default_divider_padding)
//                .setColorResource(R.color.divider_bg)
//                .build();
        GridItemDecoration divider = new GridItemDecoration.Builder(getActivity())
                .setHorizontal(R.dimen.default_divider_padding)
                .setVertical(R.dimen.default_divider_padding)
                .setColorResource(R.color.main_bg)
                .build();
        mRecyclerView.addItemDecoration(divider);
        //是否允许加载
        mRecyclerView.setLoadMoreEnabled(false);
        //是否允许刷新
        mRecyclerView.setPullRefreshEnabled(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_right_text://退出登录
                SharedPreferencesUtils.setObjectToShare(mContext, Constants.USERINFO, null);
                SharedPreferencesUtils.setParam(mContext, Constants.COOKIE, "");
                SharedPreferencesUtils.setParam(mContext, Constants.PASSWORD, "");
                SharedPreferencesUtils.setParam(mContext, Constants.ACCOUNT, "");
                //销毁所有页面和存储的数据
                getActivity().sendBroadcast(new Intent(Constants.LOGINOUT));
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
                break;

            case R.id.rl_perdion_head://修改资料
                gotoActivity(EditPersonActivity.class, false, null);
                break;
        }

    }
}
